package lin.an.food.controller;

import lin.an.food.entity.FoodDetails;
import lin.an.food.param.FoodFavoriteParam;
import lin.an.food.param.FoodToLikeParam;
import lin.an.food.service.FoodDetailsService;
import lin.an.food.service.FoodFavoriteService;
import lin.an.food.service.FoodLikeService;
import lin.an.pojo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 美食详情页
 * @author zhoulinq
 * @date 2025/4/10
 */
@RestController
@RequestMapping("/delicious/food")
public class FoodDetailsController {
    @Autowired
    FoodDetailsService foodDetailsService;
    @Autowired
    FoodLikeService foodLikeService;
    @Autowired
    FoodFavoriteService foodFavoriteService;

    /**
     * 点赞功能
     * @param foodToLikeParam 包含foodId和userId的参数对象
     * @return 点赞状态和点赞数
     */
    @PostMapping("/tolike")
    public CommonResult<Map<String, Object>> toggleLike(@RequestBody FoodToLikeParam foodToLikeParam) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 切换点赞状态并更新数据库
            boolean newLikeStatus = foodDetailsService.toggleLike(foodToLikeParam.getFoodId(), foodToLikeParam.getUserId());
            // 获取更新后的美食详情
            FoodDetails foodDetail = foodDetailsService.getFoodDetail(foodToLikeParam.getFoodId());
            
            result.put("isLiked", newLikeStatus);
            result.put("likeCount", foodDetail.getLikes());
            return CommonResult.data(result);
        } catch (Exception e) {
            return CommonResult.error("点赞操作失败：" + e.getMessage());
        }
    }

    /**
     * 获取点赞状态
     * @param foodId 美食ID
     * @param userId 用户ID
     * @return 点赞状态和点赞数
     */
    @GetMapping("/like/status")
    public CommonResult<Map<String, Object>> getLikeStatus(@RequestParam int foodId, @RequestParam int userId) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 获取点赞状态
            boolean isLiked = foodLikeService.isLiked(foodId, userId);
            // 获取美食详情
            FoodDetails foodDetail = foodDetailsService.getFoodDetail(foodId);

            result.put("isLiked", isLiked);
            result.put("likeCount", foodDetail.getLikes());
            return CommonResult.data(result);
        } catch (Exception e) {
            return CommonResult.error("获取点赞状态失败：" + e.getMessage());
        }
    }

    /**
     * 收藏功能
     * @param foodFavoriteParam 包含foodId和userId的参数对象
     * @return 收藏状态和收藏数
     */
    @PostMapping("/favorite")
    public CommonResult<Map<String, Object>> toggleFavorite(@RequestBody FoodFavoriteParam foodFavoriteParam) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 切换收藏状态并更新数据库
            boolean newFavoriteStatus = foodDetailsService.toggleFavorite(foodFavoriteParam.getFoodId(), foodFavoriteParam.getUserId());
            // 获取更新后的美食详情
            FoodDetails foodDetail = foodDetailsService.getFoodDetail(foodFavoriteParam.getFoodId());

            result.put("isFavorited", newFavoriteStatus);
            result.put("favoriteCount", foodDetail.getFavorites());
            return CommonResult.data(result);
        } catch (Exception e) {
            return CommonResult.error("收藏操作失败：" + e.getMessage());
        }
    }

    /**
     * 获取收藏状态
     * @param foodId 美食ID
     * @param userId 用户ID
     * @return 收藏状态和收藏数
     */
    @GetMapping("/favorite/status")
    public CommonResult<Map<String, Object>> getFavoriteStatus(@RequestParam int foodId, @RequestParam int userId) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 获取收藏状态
            boolean isFavorited = foodFavoriteService.isFavorited(foodId, userId);
            // 获取美食详情
            FoodDetails foodDetail = foodDetailsService.getFoodDetail(foodId);

            result.put("isFavorited", isFavorited);
            result.put("favoriteCount", foodDetail.getFavorites());
            return CommonResult.data(result);
        } catch (Exception e) {
            return CommonResult.error("获取收藏状态失败：" + e.getMessage());
        }
    }
}
